// 路由配置
import { createRouter, createWebHashHistory } from "vue-router";
import Login from '../components/login/Login.vue'
import Home from '../components/home/Home.vue'
import Order from '../components/order/Order.vue'
import Goods from '../components/good/Good.vue'
import AddGood from '../components/good/AddGood.vue'
import GoodCategory from '../components/good/GoodCategory.vue'
import ManagerList from '@/components/manager/ManagerList'
import ManagerReqList from '@/components/manager/ManagerReqList'
import ManagerOrder from '@/components/manager/ManagerOrder'
import FinancialDetail from '@/components/financial/FinancialDetail'
import FinancialOrder from '@/components/financial/FinancialOrder'
import DataCom from '@/components/financial/DataCom'
import Store from '../tools/Storage'
const Router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: Login,
            name: 'login',
            meta: {
                title: '登录'
            }
        },
        {
            path: '/home',
            component: Home,
            name: 'home',
            meta: {
                title: '首页'
            },
            children: [
                {
                    path: 'order/:type',    //0是普通订单，1是秒杀订单
                    component: Order,
                    name: "Order",
                    meta: {
                        title: '订单列表'
                    }
                },
                {
                    path: 'goods/:type',   //0是普通商品，1是秒杀商品，2是今日推荐
                    component: Goods,
                    name: "Goods",
                    meta: {
                        title: '商品列表'
                    }
                },
                {
                    path: 'addGood/:type',     //0是普通商品，1是秒杀商品，2是今日推荐
                    component: AddGood,
                    name: 'AddGood',
                    meta: {
                        title: '添加商品'
                    }
                },
                {
                    path: 'goodCategory',
                    component: GoodCategory,
                    name: 'GoodCategory',
                    meta: {
                        title: '商品分类'
                    }
                },
                {
                    path: 'ownerlist',
                    component: ManagerList,
                    name: 'ManagerList',
                    meta: {
                        title: '店长列表'
                    }
                },
                {
                    path: 'ownerreq',
                    component: ManagerReqList,
                    name: 'ManagerReqList',
                    meta: {
                        title: '店长申请审批列表'
                    }
                },
                {
                    path: 'ownerorder',
                    component: ManagerOrder,
                    name: 'ManagerOrder',
                    meta: {
                        title: '店长订单'
                    }
                },
                {
                    path: 'tradeinfo',
                    component: FinancialDetail,
                    name: 'FinancialDetail',
                    meta: {
                        title: '交易明细'
                    }
                },
                {
                    path: 'tradelist',
                    component: FinancialOrder,
                    name: 'FinancialOrder',
                    meta: {
                        title: '财务对账单'
                    }
                },
                {
                    path: 'data',
                    component: DataCom,
                    name: 'DataCom',
                    meta: {
                        title: '数据统计'
                    }
                }
            ],
            redirect: '/home/order/0'
        }        
    ]
})

// 全局路由守卫
Router.beforeEach((to,from,next) => {   
    if(to.meta.title){
        document.title = to.meta.title;
        next()
    }else{
        document.title = 'shop-admin';
        next()
    }
    // 判断是否登录
    let isLogin = Store.getters.isLogin;
    if(isLogin || from.name == 'login'){        
        return true;
    }else{
        return {name: 'login'}
    }
})

export default Router;